<?php

namespace app\Modules\Api\controllers;

use Yii;
use app\Modules\Api\models\Showorder;
use app\Modules\Api\models\Showpic;
use app\Modules\Api\models\Saleorder;
use app\filters\Json;
use app\helpers\Request;
use yii\data\Pagination;
/**
 * Class ShoworderController
 * @package app\Modules\Api\controllers
 */
class ShoworderController extends ControllerBase
{

    public function actionIndex()
    {
        /** @var  $showorder_objects Showpic*/
        $user_id = Yii::$app->request->post('user_id');
        $query = Showorder::find();
        if($user_id)
            $query->andWhere(['user_id'=>$user_id]);

        $count = $query->count();
        $pagination = new Pagination([
            'totalCount' => $count,
            'pageSize'=>Yii::$app->request->post('pageSize',10),
            'page'=>Yii::$app->request->post('page',0),
        ]);
        $showorder_objects = $query
                            ->orderBy(['updated_at'=>SORT_DESC])
                            ->offset($pagination->offset)
                            ->limit($pagination->limit)
                            ->all();
        $showorders = [];
        foreach($showorder_objects as $key =>$showorder_object)
        {
            $showorders[$key] = $showorder_object->toArray([],['showpics']);
        }

        $data = [
            'showorders' => $showorders,
            'showorder_count' =>strval($count),
        ];
        return Json::success($data);
    }
    /**
     * 产品晒单（竞拍详情）
     * @return array
     */
    public function actionAuction()
    {
        $product_id = Request::strictpost('product_id','缺少产品id');
        $query = Showorder::find()->where(['product_id'=>$product_id]);
        $count = $query->count();
        $pagination = \app\Modules\Api\helper\Pagination::get($count);
        /** @var  $showorder_objects Showpic*/
        $objects = $query->limit($pagination->limit)->offset($pagination->offset)->all();
        $showorders = [];
        foreach($objects as $key=>$object)
        {
            $showorders[$key] = $object->toArray([],['showpics']);
        }
        $data = [
            'showorders'=>$showorders,
            'showorder_count'=>strval($count),
        ];
        return Json::success($data);
    }
    /**
     * 创建
     * @return array
     */
    public function actionCreate()
    {
        $post = file_get_contents("php://input");
        file_put_contents('aaa.txt',serialize($post));
        $post = Yii::$app->request->post();
        file_put_contents('aaa.txt',serialize($post),FILE_APPEND);
        $content = Request::strictpost('content','缺少内容');
        $showpics = Yii::$app->request->post('showpics',[]);
        $order_id = Request::strictpost('order_id','缺少订单id');

        $order = Saleorder::findOne($order_id);
        if(!$order->canShow())
            return Json::error('当前订单不可晒单');
        $model = new Showorder();
        $user = $order->user;
        $model->uname = $user->name;
        $model->uimg = $user->img;
        $model->order_id = $order_id;
        $model->auction_id = $order->auction_id;
        $model->product_id = $order->product_id;
        $model->user_id = $order->user_id;
        $model->content = $content;

        if (!$model->save()) {
            return Json::error(strval(current($model->getFirstErrors())));
        }

        $order->state = Saleorder::STEAT_POST;
        $order->save(false);
        $showpic = new Showpic();
        $showpic->auction_id = $order->auction_id;
        $showpic->product_id = $order->product_id;
        $showpic->user_id = $order->user_id;
        $showpic->show_id = $model->showid;
        if(is_string($showpics)){
            $showpics = json_decode($showpics);
        }
        foreach($showpics as $url)
        {
            $clonepic = clone $showpic;
            $clonepic->url = $url;
            $clonepic->save(false);
        }
        return Json::success();
    }
}